package lgs.hm.aop2;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class JingChaAop {
	
	@Pointcut("execution(* lgs.hm.aop2.Person.*(..))") //与或非
	public void zq() {}
	
	@Pointcut("@annotation(lgs.hm.aop2.DaGu)")  //所有标注了这个注解的方法都要拦截
	public void dg() {}
	
//	@Pointcut("@annotation(lgs.hm.aop2.FromFile)")
//	public void FromFile() {}
	
	
	
	@Around("@annotation(ff)")
	public Object round(ProceedingJoinPoint pjp,FromFile ff) {
		String path = ff.value();
//		System.out.println(ff.value());
//		MethodSignature signature = (MethodSignature) pjp.getSignature();
//		Method method = signature.getMethod();
//	    lgs.hm.aop2.FromFile annotation = method.getAnnotation(FromFile.class);
//	    String value = annotation.value();
//	    System.out.println(value);
		
		Object[] args = pjp.getArgs();
		
		try(FileReader fr = new FileReader(ff.value());
				BufferedReader br = new BufferedReader(fr)
				) {
			String str = "";
			StringBuilder sb = new StringBuilder();
			while((str=br.readLine())!=null) {
				sb.append(str);
			}
//			if(Math.random()<0.333)return -100000;
			
			return pjp.proceed(new Object[] {sb.toString()});
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return null;
		
	}

}
